package com.autocar.reader.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.autocar.reader.R;
import com.autocar.reader.bean.CommentSubBean;
import com.autocar.reader.net.ImageLoader;
import com.autocar.reader.net.ImageLoader.ImageCallback;
import com.autocar.reader.util.ConstantData;
import com.autocar.reader.util.ImageUtils;
import com.autocar.reader.view.XListView;
import com.org.any.autocar.ChatLookPersonAddActivity;
import com.org.any.autocar.fragment.IAuthorClickListener;
import com.org.any.autocar.fragment.IopenArticle;

public class CommentAdapter extends BaseAdapter implements ImageCallback {

	private LayoutInflater inflater;
	private List<CommentSubBean> listnews;
	private XListView rootView;
	private ImageLoader load;
	private IAuthorClickListener iopen;
	private Context context = null;

	public CommentAdapter(Context context, XListView rootView,
			IAuthorClickListener iopen) {
		inflater = LayoutInflater.from(context);
		this.rootView = rootView;
		load = new ImageLoader(this);
		this.iopen = iopen;
		this.context = context;
	}

	public void setListComm(List<CommentSubBean> listnews) {
		this.listnews = listnews;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listnews == null ? 0 : listnews.size();
	}

	@Override
	public CommentSubBean getItem(int position) {
		return listnews.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHodler holder;
		if (convertView == null) {
			holder = new ViewHodler();
			convertView = inflater.inflate(R.layout.comment_list_item_layout,
					null);
			holder.timeTv = (TextView) convertView
					.findViewById(R.id.comment_list_tv_time);
			holder.contentTv = (TextView) convertView
					.findViewById(R.id.comment_list_tv_content);
			holder.nameTv = (TextView) convertView
					.findViewById(R.id.comment_list_tv_name);
			holder.picIv = (ImageView) convertView
					.findViewById(R.id.comment_list_iv_pic);
			convertView.setTag(holder);
		} else {
			holder = (ViewHodler) convertView.getTag();
		}

		final CommentSubBean bean = getItem(position);
		holder.contentTv.setText(bean.getContent());
		holder.nameTv.setText(bean.getUsername());
		holder.timeTv.setText(bean.getAdd_time());
		// final String url = bean.getUser_profile();
		// holder.picIv.setTag(url + position);
		// String filePath = ConstantData.profileFile(bean.getUid());
		// Bitmap bitmap = ConstantData.getBitmapBypath(filePath);
		// holder.picIv.setImageBitmap(bitmap);
		// if (bitmap == null) {
		// holder.picIv.setImageResource(R.drawable.no_img_0);
		// load.loadDrawable(url, filePath);
		// }
		ImageUtils.fb.display(holder.picIv, bean.getUser_profile());
		holder.picIv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				iopen.toAuthor(bean.getUid());
			}
		});
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				iopen.toReComment(bean);
			}
		});

		return convertView;
	}

	class ViewHodler {
		TextView nameTv;
		TextView contentTv;
		TextView timeTv;
		ImageView picIv;
	}

	@Override
	public void imageLoaded(String filePath, String imageUrl) {
		ImageView view = (ImageView) rootView.findViewWithTag(imageUrl);
		if (view != null) {
			view.setImageBitmap(ConstantData.getBitmapBypath(filePath));
		}
	}

}
